var wxUtil = require('wxUtil');
var shareConfig = require('shareConfig');
var eventConfig = require('eventConfig');

cc.Class({
    extends: cc.Component,

    properties: {
        display:{
            default: null,
            type: cc.Sprite
        },
        sClick:{
            default:null,
            url:cc.AudioClip
        },
        spaceTime: 1.5,
        lbScore:cc.Label,
    },

    onLoad () {
        let openDataContext = wx.getOpenDataContext();
        openDataContext.postMessage({message:'GET_TOP_BY_DLG_END'});
        //只能在主域设置大小, 且要先于赋值到sprite才起作用
        var sharedCanvas = openDataContext.canvas;
        sharedCanvas.width = this.display.node.width;
        sharedCanvas.height = this.display.node.height;
        this.t_count =0;
        //
        // wxUtil.hideShareMenu();
        //
        wxUtil.showBanner();
    },
    start () {
        this.tex = new cc.Texture2D();
    },
    setShow(score){
        this.node.active = true;
        this.lbScore.string = '本次分数 : '+score
        // let high = Number(gGlobal.getHight());
        let openDataContext = wx.getOpenDataContext();
        // openDataContext.postMessage({message:'UPDATE_HIGH_SCORE',h_value:high,c_value:score});
        openDataContext.postMessage({message:'UPDATE_HIGH_SCORE',c_value:score});
    },
    update (dt) {
        if(this.node.active){
            this._updaetSubDomainCanvas();
        }
        
    },
    _updaetSubDomainCanvas () {
        if (!this.tex) {
            return;
        }
        var openDataContext = wx.getOpenDataContext();
        var sharedCanvas = openDataContext.canvas;
        // console.log("sharedCanvas.width:"+sharedCanvas.width+",height:"+sharedCanvas.height);
        this.tex.initWithElement(sharedCanvas);
        this.tex.handleLoadedTexture();
        this.display.spriteFrame = new cc.SpriteFrame(this.tex);
    },
    setCallFun(cf){
        this._cf = cf;
    },
    btnAgain:function(){
        if(gGlobal.gVoice)
            cc.audioEngine.play(this.sClick,false);
        cc.director.loadScene('sceneGame');
    },
    btnShare:function(){
        // cc.director.loadScene('game');
        if(gGlobal.gVoice)
            cc.audioEngine.play(this.sClick,false);
        let url = shareConfig.url;
        let index = Math.floor(Math.random()*10)%shareConfig.list.length;
        wxUtil.shareAppMessage(shareConfig.list[index].s_txt,url+shareConfig.list[index].s_url);
    },

    btnRank:function(){
        if(gGlobal.gVoice)
            cc.audioEngine.play(this.sClick,false);
        if(this._cf){
            this._cf();
        }else{
            console.log("gameOver--cf is null");
        }
    },

});
